package com.ljf.framework.security;

import com.ljf.framework.LjfManager;
import com.ljf.framework.cache.LjfCacheManager;
import com.ljf.framework.cache.LjfCacheService;
import com.ljf.framework.exception.LjfException;
import com.ljf.framework.exception.LjfUserInfoIncorrectException;
import com.ljf.framework.log.LjfLogManager;
import com.ljf.framework.log.LjfLogService;
import com.ljf.framework.result.JsonResult;
import com.ljf.framework.result.JsonResultEnum;
import com.ljf.framework.security.token.LjfTokenManager;
import com.ljf.framework.security.token.LjfTokenConstants;
import com.ljf.framework.util.LjfStringUtil;
import com.ljf.framework.util.ObjectUtil;

import java.util.UUID;

/**
 * 说明：
 *
 * @Auther: lijinfeng
 * @Date: 2023/9/18
 */
public class LjfSecurityServiceDefaultImpl implements LjfSecurityService {

    LjfLogService log = LjfLogManager.getLogger(LjfSecurityServiceDefaultImpl.class);

    @Override
    public JsonResult doLogin(String username, String password) {
        Long userId = LjfSecurityManager.getConfig().getCheckUserHandler().apply(username, password);
        if (0 == userId) {
            throw new LjfUserInfoIncorrectException("用户信息错误");
        }
        LjfSecurityManager.login(userId);
        log.info("用户[" + userId + "]登录成功。");
        return JsonResult.success(LjfTokenManager.getLjfToken());
    }


    @Override
    public JsonResult doLogout() {
        LjfCacheService ljfGameCacheService = LjfManager.getLjfCacheService();
        String tokenValue = LjfTokenManager.getTokenValue();
        Long userId = LjfSecurityManager.getUserId();
        log.info("用户[" + userId + "]退出成功。");
        ljfGameCacheService.delete(LjfTokenConstants.TOKEN_PREFIX + tokenValue);
        return ljfGameCacheService.get(LjfTokenConstants.TOKEN_PREFIX + tokenValue) == null ? JsonResult.success("登出成功") : JsonResult.success("未登录");
    }

    @Override
    public boolean isLogin() {
        LjfCacheService ljfGameCacheService = LjfManager.getLjfCacheService();
        String tokenValue = LjfTokenManager.getTokenValue();
        Object select = ljfGameCacheService.get(LjfTokenConstants.TOKEN_PREFIX + tokenValue);
        return select != null;
    }

    @Override
    public String getUsername() {
        String tokenValue = LjfTokenManager.getTokenValue();
        return getUsername(tokenValue);
    }


    @Override
    public String getUsername(String tokenValue) {
        LjfCacheService ljfGameCacheService = LjfManager.getLjfCacheService();
        Object select = ljfGameCacheService.get(LjfTokenConstants.TOKEN_PREFIX + tokenValue);
        if(ObjectUtil.isEmpty(select))
            throw new LjfException(JsonResultEnum.TOKEN_INVALID);
//        HashMap map = LjfMapUtil.objectStringToMap(select.toString());
//        String username = (String) map.get("username");
        // TODO 前期没有存储username信息 这里现用userId代替
        Long userId = Long.valueOf(select.toString());

        return userId.toString();
    }

    @Override
    public Long getUserId() {
        // 缓存 userId -token 对应关系
        String tokenValue = LjfTokenManager.getTokenValue();
        String s = LjfCacheManager.get(LjfTokenConstants.TOKEN_PREFIX + tokenValue);
        if(LjfStringUtil.isEmpty(s)){
            throw new LjfException("token 无效，没有该token值");
        }
        return Long.parseLong(s);
    }

    @Override
    public String createLjfToken(Long userId) {
        String token = UUID.randomUUID().toString();
        return token;
    }


}
